<?php
$sikoka=20;
$anticapkey='key';
$dir='/var/php/ya.ru/';
for($i=0; $i<$sikoka; $i++) {
	$mail  = get_random_string(15);
	$fname = get_random_string(5);
	$lname = get_random_string(5);
	$pass  = get_random_string(8);
	$hinta = get_random_string(7);

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://passport.yandex.ru/passport?mode=register');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,'step=one&filled=yes&iname='.$lname.'&fname='.$fname.'&login='.$mail.'&done=%C4%E0%EB%FC%F8%E5%A0%26%238594%3B');
	curl_setopt($ch, CURLOPT_HEADER, 1);

	$result = curl_exec($ch);
	preg_match('/Set-Cookie: (.*) path=/i', $result, $match);
	$cookie=$match[1];
	preg_match('/<img src="(.*)" name="captcha" border=0 width=200 h/i', $result, $match);
	$urlcap=$match[1];
	preg_match('/<input type="hidden" name="idkey" value="(.*)">/i',$result, $match);
	$idkey=$match[1];
	# print $urlcap."\n";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,"http://passport.yandex.ru".$urlcap);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	$result = curl_exec($ch);
	$fp=fopen('cap.png','w');
	fwrite($fp, $result);
	fclose($fp);
	$text=recognize($dir."cap.png",$anticapkey,false,7,120,0,6);
	#    print("|||".$text."|||");
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://passport.yandex.ru/passport?mode=regone');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_POST, 1);
	$post='idkey='.$idkey.'&filled=yes&iname='.$lname.'&fname='.$fname.'&login='.$mail.'&passwd='.$pass.'&passwd2='.$pass.'&hintq=2&udhintq=&hinta='.$hinta.'&hintasave='.$hinta.'&questnum=2&hintqsave=&email=&phonenumber=&code='.$text.'&agreed=yes&done=%C7%E0%F0%E5%E3%E8%F1%F2%F0%E8%F0%EE%E2%E0%F2  %FC';
	curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
	$result = curl_exec($ch);
	if(preg_match("/Set-Cookie: yandex_login/i", $result)) {
		print $mail.":".$pass."@ya.ru\n";
	} else {
		print "error\n";
	}
}

function recognize($filename, $apikey, $is_verbose = true, $rtimeout = 5, $mtimeout = 120, $is_phrase = 0, $is_regsense = 0, $is_numeric = 0, $min_len = 0, $max_len = 0)
{
	if (!file_exists($filename)) {
		if ($is_verbose) echo "file $filename not found\n";
		return false;
	}
	$postdata = array(
        'method'    => 'post', 
        'key'       => $apikey, 
        'file'      => '@'.$filename,
	    'phrase'    => $is_phrase,
        'regsense'  => $is_regsense,
        'numeric'   => $is_numeric,
        'min_len'   => $min_len,
        'max_len'   => $max_len
	);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://www.anti-captcha.com/in.php');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
	$result = curl_exec($ch);
	if (curl_errno($ch)) {
		if ($is_verbose) echo "CURL returned error: ".curl_error($ch)."\n";
		return false;
	}
	curl_close($ch);
	if (strpos($result, "ERROR") !== false) {
		if ($is_verbose) echo "server returned error: $result\n";
		return false;
	} else {
		$ex = explode("|", $result);
		$captcha_id = $ex[1];
		if ($is_verbose) echo "captcha sent, got captcha ID $captcha_id\n";
		$waittime = 0;
		if ($is_verbose) echo "waiting for $rtimeout seconds\n";
		sleep($rtimeout);
		while(true) {
			$result = file_get_contents('http://anti-captcha.com/res.php?key='.$apikey.'&action=get&id='.$captcha_id);
			if (strpos($result, 'ERROR')!==false) {
				if ($is_verbose) echo "server returned error: $result\n";
				return false;
			}
			if ($result == "CAPCHA_NOT_READY") {
				if ($is_verbose) echo "captcha is not ready yet\n";
				$waittime += $rtimeout;
				if ($waittime > $mtimeout) {
					if ($is_verbose) echo "timelimit ($mtimeout) hit\n";
					break;
				}
				if ($is_verbose) echo "waiting for $rtimeout seconds\n";
				sleep($rtimeout);
			}
			else {
				$ex = explode('|', $result);
				if (trim($ex[0])=='OK') return trim($ex[1]);
			}
		}
		return false;
	}
}

function get_random_string( $len ) {
	$result = "";
	$nums = "1234567890";
	$syms = "abcdefghijklmnopqrstuvwxyz";
	$sux = $nums.$syms;

	for ($i = 0; $i <= $len; $i++ ) {
		$num = rand( 0, strlen( $sux ) - 1 );
		$result .= $sux[ $num ];
	}
	return $syms[ rand(0,strlen( $syms ) - 1 ) ].$result;
}
?>